<?php
/**
 * 公共数据处理
 * @author cjli
 *
 */
class PublicAction extends HomeAction
{
	//js获取价格格式
	public function paymentc()
	{
		$price = floatval($_GET['price']);
		echo currency($price);
	}
	
	//切换货币
	public function changecurrency()
	{
		$currency = trim($_GET['currency']);
		$currencyList = SiteModel::currency_list();
		if($currencyList[$currency]){
			$_SESSION['currency']    = $currency;
			$_SESSION['curr_change'] = 1;
		}

	}
	
	/**
	 * 获取会员地址信息
	 */
    /**
     * 获取会员地址信息
     */
    public function getaddressinfo1($address_id){
        global $user;
        if(!UserModel::logged()){
            exit(0);
        }
        $address_id = $address_id? intval($address_id) : 0;
        return  $addressInfo = UserModel::get_address_info($address_id);
    }
    public function getaddressinfo()
    {

        $addressInfo=$this->getaddressinfo1($_GET['address_id']);
        echo $addressInfo['delivery_cid'] ? $addressInfo['delivery_cid'] : 0;
        exit;
    }
	
	/**
	 * 生成验证码
	 */
	public function vcode()
	{
			import("COM.CheckCode");
			$vcode_session_name = trim($_GET['vcode_name']);
			$vcode_session_name = $vcode_session_name ? $vcode_session_name : 'vcode';
    	$checkcode = new CheckCode();
    	if (isset($_GET['code_len']) && intval($_GET['code_len'])) $checkcode->code_len = intval($_GET['code_len']);
			if ($checkcode->code_len > 8 || $checkcode->code_len < 2) {
				$checkcode->code_len = 4;
			}
			if (isset($_GET['font_size']) && intval($_GET['font_size'])) $checkcode->font_size = intval($_GET['font_size']);
			if (isset($_GET['width']) && intval($_GET['width'])) $checkcode->width = intval($_GET['width']);
			if ($checkcode->width <= 0) {
				$checkcode->width = 130;
			}
			if (isset($_GET['height']) && intval($_GET['height'])) $checkcode->height = intval($_GET['height']);
			if ($checkcode->height <= 0) {
				$checkcode->height = 50;
			}
			if (isset($_GET['font_color']) && trim(urldecode($_GET['font_color'])) && preg_match('/(^#[a-z0-9]{6}$)/im', trim(urldecode($_GET['font_color'])))) $checkcode->font_color = trim(urldecode($_GET['font_color']));
			if (isset($_GET['background']) && trim(urldecode($_GET['background'])) && preg_match('/(^#[a-z0-9]{6}$)/im', trim(urldecode($_GET['background'])))) $checkcode->background = trim(urldecode($_GET['background']));
			$checkcode->doimage();
			$_SESSION[$vcode_session_name] = $checkcode->get_code();
	}
	
	/**
	 * ajax验证-验证码
	 */
	public function vcode_check()
	{
		$vcode = trim($_REQUEST['vcode']);
		$vcode_name = trim($_REQUEST['vcode_name']);
		if($_SESSION[$vcode_name] != strtolower($vcode)){
			echo 0;exit;
		}else{
			echo 1;exit;
		}
	}
	
	/**
	 * swfupload 上传图片
	 */
	public function swfupload_ajax()
	{
		$subdir = date('Y/m/d/', time());
		import("ORG.Net.UploadFile");
		$upload = new UploadFile();// 实例化上传类
		$upload->maxSize  = C('UPLOAD_SIZE') ;// 设置附件上传大小
		$upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$upload->savePath =  '.'.UPLOAD_PATH;// 设置附件上传目录
		$upload->autoSub = true;//开启子目录
		$upload->subType = 'custom';// 子目录创建方式 可以使用hash date custom
		$upload->subDir = $subdir;
		$upload->thumb = true;//开启缩略图
		$upload->thumbType = C('product_thumb_type') ? intval(C('product_thumb_type')) : 0;
		$upload->thumbMaxWidth = C('product_list_width').','.C('product_thumb_width');
		$upload->thumbMaxHeight = C('product_list_height').','.C('product_thumb_height');
//		$upload->thumbMaxWidth = C('product_thumb_width');
//		$upload->thumbMaxHeight = C('product_thumb_height');
		$upload->thumbPrefix = 'list_,thumb_';
		if(!$upload->upload()) {// 上传错误提示错误信息
			echo $upload->getErrorMsg();
		}else{// 上传成功 获取上传文件信息
			$photo_db = D('product_photo');
			$photoArray = array();
			$files = $upload->getUploadFileInfo();
			foreach ($files as $file){
				$img = $file['savename'];
				$data['img'] = UPLOAD_PATH.$img;
				$insertId = $photo_db->add($data);
				if($insertId){
					$thumb = 'thumb_'.basename($img);
					echo UPLOAD_PATH.$subdir.$thumb.'|'.UPLOAD_PATH.$subdir.$thumb.'|'.UPLOAD_PATH.$img.'|'.$insertId;exit;
				}else{
					exit('0');
				}
			}
	 	}
	}
	
	public function newsletter()
	{
	
		$this->display('newsletter');
	}
	
	public function unsubscribe()
	{
		if(isset($_POST['dosubmit'])){
			if($_SESSION['registry_code'] != $_POST['registry_code']){
				$this->error(L('_VERIFICATION_ERROR_'),  url('public/unsubscribe'));
			}
			$email = trim($_POST['email']);
			import('COM.Validate');
			$validate = new Validate();
			if(!$validate->email($email)){
				$this->error(L('_EMAIL_FORMAT_ERROR_'), url('public/unsubscribe'));
			}else{
				$email_registry = D('email_registry');
				$data = array('email' => $email);
				$res = $email_registry->where($data)->find();
				if(!$res){
					$this->error(L('_EMAIL_NO_SUBSCRIBE_'), url(''));
				}else{
					if($res['status'] == 1){
						$email_registry->where('id='.$res['id'])->save(array('status' => 0));
					}
					$this->error(L('_UNSUBSCRIBE_SUCCESS_'), url(''));
				}
			}
		}else{
			$this->display('newsletter');
		}
	}
	
	/**
	 * 邮件订阅收集邮箱
	 */
	public function newsletter_ajax()
	{
		$result = array(
			'error' => 0,
			'message' => '',
		);
		
		$email = trim($_REQUEST['email']);
		import('COM.Validate');
		$validate = new Validate();
		if(!$validate->email($email)){
			$message = L('_EMAIL_FORMAT_ERROR_');
			$result['error'] = 1;
		}else{
			$email_registry = D('email_registry');
			$data = array('email' => $email);
			$res = $email_registry->where($data)->find();
			if(!$res){
				$id = $email_registry->add($data);
				if($id){
					$message = L('_NEWSLETTER_SUBSCRIPTION_SUCCESS_');
				}else{
					$message = L('_NEWSLETTER_SUBSCRIPTION_FAILURE_');
					$result['error'] = 1;
				}
			}else{
				if($res['status'] == 0){
					$email_registry->where('id='.$res['id'])->save(array('status' => 1));
				}
				$message = L('_NEWSLETTER_SUBSCRIPTION_SUCCESS_');
			}
		}
		$result['message'] = $message;
		echo json_encode($result);
	}
	
	//邮件退订
	public function unsubscribe_ajax()
	{
		$result = array(
			'error' => 0,
			'message' => '',
		);
		
		$email = trim($_REQUEST['email']);
		import('COM.Validate');
		$validate = new Validate();
		if(!$validate->email($email)){
			$message = L('_EMAIL_FORMAT_ERROR_');
			$result['error'] = 1;
		}else{
			$email_registry = D('email_registry');
			$data = array('email' => $email);
			$res = $email_registry->where($data)->find();
			if(!$res){
				$message = L('_EMAIL_NO_SUBSCRIBE_');
			}else{
				if($res['status'] == 1){
					$email_registry->where('id='.$res['id'])->save(array('status' => 0));
				}
				$message = L('_UNSUBSCRIBE_SUCCESS_');
			}
		}
		$result['message'] = $message;
		echo json_encode($result);
	}
}